from flask import render_template

from apps.background import background_bp
from models.users import User
from utils.decorators import login_required, permission_required


@background_bp.route('/')
@login_required
@permission_required('BACKGROUND')
def index():
    """后台首页视图"""
    return render_template('background/index.html')


@background_bp.route('/users_list')
@login_required
@permission_required('BACKGROUND')
def users_list():
    """后台用户列表视图"""
    users = User.query.all()
    return render_template('background/users_list.html', users=users)


@background_bp.route('/jquery_test')
@login_required
@permission_required('BACKGROUND')
def jquery_test():
    """后台首页视图"""
    return render_template('background/jquery_test.html')


@background_bp.route('/test')
@login_required
@permission_required('BACKGROUND')
def test():
    """后台首页视图"""
    users = User.query.all()
    context = {
        "users_info": [],
        "msg": "ok"
    }
    for user in users:
        context["users_info"].append(
            {
                "id": user.id,
                "username": user.username,
                "email": user.email
            }
        )

    return context
